package com.coocaa.ops.admin.v1.tab.provider.impl;

import com.coocaa.ops.admin.v1.panel.mapper.IPanelInfoMapper;
import com.coocaa.ops.admin.v1.plugin.biz.IPluginInfoBiz;
import com.coocaa.ops.admin.v1.tab.biz.impl.TabInfoBizImpl;
import com.coocaa.ops.admin.v1.tab.convert.TabPanelRlsQueryConverter;
import com.coocaa.ops.admin.v1.tab.model.TabInfoModel;
import com.coocaa.ops.admin.v1.tab.model.query.TabPanelQueryModel;
import com.coocaa.ops.admin.v1.tab.provider.ITabInfoQueryProvider;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.util.PageQueryUtil;
import com.coocaa.ops.common.core.biz.tab.constant.TabTypeConstants;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import com.coocaa.ops.common.core.biz.tab.entity.TabPanelConfigInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author bjh
 * @description 版面信息原始表-查询provider接口实现
 * @date : 2020-06-15 16:27:18
 */
@Service
public class TabInfoQueryProviderImpl implements ITabInfoQueryProvider {
    @Autowired
    private TabInfoBizImpl thisBizImpl;
    @Autowired
    private IPanelInfoMapper panelInfoBiz;
    @Autowired
    private IPluginInfoBiz pluginInfoBiz;


    /**
     * 获取详情信息
     *
     * @param id
     * @param version
     * @return
     */
    @Override
    public TabInfoEntity getDetailInfo(Integer id, Integer version) {
        TabInfoEntity entity = this.getEntityByIdAndVersion(id, version);
        if(TabTypeConstants.isNormal(entity.getTabType())){
            return queryNormalTab(entity);
        }else if(TabTypeConstants.isVideo(entity.getTabType())){
            return queryVideoTab(entity);
        } else if (TabTypeConstants.isPlugin(entity.getTabType())) {
            return queryPluginTab(entity);
        }else if(TabTypeConstants.isPoster(entity.getTabType())) {
            return queryPosterTab(entity);
        }
        return entity;
    }

    /**
     * 分页数据-菜单自己使用
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<TabInfoEntity> pageList(TabInfoModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        PageResultModel<TabInfoEntity> pageResult = thisBizImpl.getCommonPageList(params, true);
        return pageResult;
    }

    /**
     * 分页数据-其他处选择
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<TabInfoEntity> dataList(TabInfoModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        PageResultModel<TabInfoEntity> pageResult = thisBizImpl.getCommonPageList(params, false);
        return pageResult;

    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    /**
     * 获取基础信息-根据ID和版本
     *
     * @param id
     * @param version
     * @return
     */
    private TabInfoEntity getEntityByIdAndVersion(Integer id, Integer version) {
        return thisBizImpl.getEntityByIdAndVersion(id, version);

    }

    private TabInfoEntity queryVideoTab(TabInfoEntity entity) {
        return entity;
    }

    private TabInfoEntity queryNormalTab(TabInfoEntity entity) {
        TabPanelQueryModel model = new TabPanelQueryModel(entity.getId(), entity.getCurrentVersion());
        List<TabPanelConfigInfo> commonPanelList = panelInfoBiz.queryCommonPanelList(model);
        List<TabPanelConfigInfo> dmpPanelList = null;
        if (Objects.equals(1, entity.getHasDmp())) {
            dmpPanelList = panelInfoBiz.queryDmpPanelList(model);
        }
        entity.setPanelList(TabPanelRlsQueryConverter.convertFinalPanelList(commonPanelList, dmpPanelList));
        return entity;
    }

    private TabInfoEntity queryPluginTab(TabInfoEntity tab) {
        if (tab.getPluginSelfId() == null) {
            return tab;
        } else {
            tab.setPluginInfo(pluginInfoBiz.queryById(tab.getPluginSelfId()));
            return tab;
        }
    }

    private TabInfoEntity queryPosterTab(TabInfoEntity tab) {
        return tab;
    }
}
